package com.wsc.it.parking;

import java.nio.charset.StandardCharsets;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wsc.it.parking.enums.JslifeInterfaceEnum;
import com.wsc.it.parking.vo.JslifeLoginRequest;
import com.wsc.it.parking.vo.JslifeLoginResponse;
import com.wsc.it.parking.vo.JslifeRequestBase;
import com.wsc.it.utils.MD5Utils;
import com.wsc.it.utils.PrinterUtil;

public class JslifeUtil {
	private static final Logger log = LoggerFactory.getLogger(PrinterUtil.class);

    /**
     * 根据retCode判断是否可以起闸出场
     */
    public static boolean isCarExit(String retCode) {
        if ("0".equals(retCode) || "1004".equals(retCode) || "1006".equals(retCode) ||
                "1007".equals(retCode) || "1008".equals(retCode) || "1009".equals(retCode) ||
                "1010".equals(retCode) || "1011".equals(retCode)) {
            return true;
        }
        return false;
    }

    /**
     * 根据 key拿value
     * @param retCode
     * @return
     */
    public static String retCodeDict(String retCode) {
        JslifeInterfaceEnum math = JslifeInterfaceEnum.math(Integer.parseInt(retCode));
        return math.getMessage();
    }

    /**
     * 调用捷顺api接口
     * @param requestBody
     * @param cid
     * @return
     */
    public static String jslifeAs(Object requestBody, String cid) {
        //登录
        JslifeLoginResponse loginResponse = jslifeLogin(cid);
        if (loginResponse.getResultCode() == 0) {
            //根据cid 获取密钥请求捷顺api
            String signKey = "";
            if (JslifeInterfaceEnum.PARKING001_CID.getUrl().equals(cid)) {
                signKey = JslifeInterfaceEnum.PARKING001_SIGNKEY.getUrl();
            }
            if (JslifeInterfaceEnum.PARKING002_CID.getUrl().equals(cid)) {
                signKey = JslifeInterfaceEnum.PARKING002_SIGNKEY.getUrl();
            }

            //捷顺接口request
            JslifeRequestBase requestBase = new JslifeRequestBase();
            requestBase.setCid(cid);
            requestBase.setTn(loginResponse.getToken());
            requestBase.setV("2");
            String requestBodyJson = JSON.toJSONString(requestBody);
            requestBase.setP(requestBodyJson);
            try {
                String sn = MD5Utils.md5(requestBodyJson, signKey).toUpperCase();
                requestBase.setSn(sn);
            } catch (Exception e) {
                log.error("MD5加密异常 {}", e.getMessage());
            }
            //调用捷顺接口
            String responeJson = jslifeHttpPost(requestBase, JslifeInterfaceEnum.PROD_URL.getUrl());
            return responeJson;
        } else {
            log.error("登录失败  message{}", loginResponse.getMessage());
        }
        return null;
    }

    /**
     * 捷顺登录
     *
     * @param cid
     * @return
     */
    public static JslifeLoginResponse jslifeLogin(String cid) {
        JslifeLoginRequest loginRequest = null;
        //根据客户号cid 登录获取token
        if (JslifeInterfaceEnum.PARKING001_CID.getUrl().equals(cid)) {
            loginRequest = new JslifeLoginRequest();
            loginRequest.setCid(JslifeInterfaceEnum.PARKING001_CID.getUrl());
            loginRequest.setUsr(JslifeInterfaceEnum.PARKING001_USR.getUrl());
            loginRequest.setPsw(JslifeInterfaceEnum.PARKING001_PSW.getUrl());
        }
        if (JslifeInterfaceEnum.PARKING002_CID.getUrl().equals(cid)) {
            loginRequest = new JslifeLoginRequest();
            loginRequest.setCid(JslifeInterfaceEnum.PARKING002_CID.getUrl());
            loginRequest.setUsr(JslifeInterfaceEnum.PARKING002_USR.getUrl());
            loginRequest.setPsw(JslifeInterfaceEnum.PARKING002_PSW.getUrl());
        }
        if (loginRequest != null) {
            String url = JslifeInterfaceEnum.LOGIN_URL.getUrl();
            String responseJson = jslifeHttpPost(loginRequest, url);
            JslifeLoginResponse loginResponse = JSONObject.toJavaObject(JSON.parseObject(responseJson), JslifeLoginResponse.class);
            return loginResponse;
        }
        return null;
    }

    public static String jslifeHttpPost(Object requestBase, String postUrl) {
        String requstJson = JSON.toJSONString(requestBase);
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(postUrl);
        post.addHeader("Content-type", "application/json; charset=utf-8");
        post.setHeader("Accept", "application/json");
        post.setEntity(new StringEntity(requstJson, StandardCharsets.UTF_8));
        try {
            HttpResponse httpResponse = client.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            String s = EntityUtils.toString(entity);
            log.info("捷顺接口: {}  request: {} \r\n response: {}", postUrl, requstJson, s);
            return s;
        } catch (Exception e) {
            log.error("捷顺接口: {} 异常: {}", postUrl, e.getMessage());
        }
        return null;
    }
}
